/*
 * MainMenu.java
 *
 * Created on 13 de Junho de 2007, 21:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.view;

import com.EntertainerMidlet;
import com.view.components.CanvasColor;
import com.view.components.CanvasForm;
import com.view.components.CanvasListGroup;
import com.view.components.CanvasListItem;
import com.view.components.ImageUtil;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Gustavo
 */
public class MainMenu extends CanvasForm implements Runnable {
    
    CanvasListGroup menuList;
    
    Image teatroImg = null, teatroImgSel = null;
    
    Image cinemaImg = null, cinemaImgSel = null;
    
    EntertainerMidlet midlet;
    
    private Image imgTitle;
    
    private int[] rawInt;
    
    private Image imgSel;
    
    private boolean mTrucking = true;
    
    private boolean mUpdate = true;
    
    private boolean firstTime = true;
    
    private int mAlpha = 0;
    
    private int mValue = 5;
    
    private Image mBufferImage;
    
    private Graphics mGraphics;
    
    private Graphics g;
    
    Thread t;
    
    /** Creates a new instance of MainMenu */
    public MainMenu(EntertainerMidlet midlet) {
        super(midlet);
        
        this.midlet = midlet;
        
        this.addOkAndBack();
        this.setCommandListener(this);
        
        try {
            this.backgroundImg = Image.createImage("/imgs/background.png");
            teatroImg = Image.createImage("/imgs/teatro.png");
            teatroImgSel = Image.createImage("/imgs/teatro_selected.png");
            cinemaImg = Image.createImage("/imgs/cinema.png");
            cinemaImgSel = Image.createImage("/imgs/cinema_selected.png");
            
            imgTitle = Image.createImage("/imgs/title.png");
            
            // Load images. Use mGraphics to draw on the backbuffer.
            mBufferImage = Image.createImage(imgTitle.getWidth(), imgTitle.getHeight());
            mGraphics = mBufferImage.getGraphics();
            
            // Allocate mamory for the image array.
            // Use the getRGB method to get all ARGB values from the image to the rawInt array.
            rawInt = new int[imgTitle.getWidth() * imgTitle.getHeight()];
            imgTitle.getRGB(rawInt, 0, imgTitle.getWidth(), 0, 0, imgTitle.getWidth(), imgTitle.getHeight());
            
            t = new Thread(this);
            t.start();
            
            menuList = new CanvasListGroup(null,30,imgTitle.getHeight() + 10,false);
            
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public CanvasListGroup getMenuList() {
        return menuList;
    }
    
    protected void paint(Graphics g) {
        this.g = g;
        
        g.drawImage(this.backgroundImg,0,0,g.TOP|g.LEFT);
        
        g.drawImage(mBufferImage, 0, 0, g.TOP|g.LEFT);
        
        mUpdate = true;
        
        menuList.g = g;
        menuList.width = getWidth();
        
        if(menuList.items == null) {
            CanvasListItem itemTeatro = new CanvasListItem(g,"",g.TOP|g.LEFT,new CanvasColor(0x000000),new CanvasColor(0xFFFFFF));
            
            itemTeatro.imgIcon = teatroImg;
            itemTeatro.imgIconSelected = teatroImgSel;
            itemTeatro.id = MenuConstants.TEATRO;
            
            menuList.addItem(itemTeatro);
            
            CanvasListItem itemCinema = new CanvasListItem(g,"",g.TOP|g.LEFT,new CanvasColor(0x000000),new CanvasColor(0xFFFFFF));
            
            itemCinema.imgIcon = cinemaImg;
            itemCinema.imgIconSelected = cinemaImgSel;
            itemCinema.id = MenuConstants.CINEMA;
            menuList.addItem(itemCinema);
        }
        
        menuList.paint();
    }
    
    protected void keyPressed(int keyCode) {
        String key = getKeyName(keyCode);
        menuList.keyPressedList(key);
        
        if(key.equals("SELECT")) {
            this.commandAction(cmdOk,this);
        }
        
        repaint();
    }

    protected void keyReleased(int keyCode) {
    }    
    
    public void commandAction(Command cmd, Displayable displayable) {
        if(cmd == cmdOk) {
            if(menuList.selectedItem.id == MenuConstants.CINEMA) {
                if(midlet.formServices == null) {
                    midlet.formServices = new ServicesMenu(midlet);
                }
                
                t.yield();
                
                midlet.display.setCurrent(midlet.formServices);
            } else {
                if(menuList.selectedItem.id == MenuConstants.TEATRO) {
                    if(midlet.formServices == null) {
                        midlet.formServices = new ServicesMenu(midlet);
                    }
                    
                    t.yield();
                    
                    midlet.display.setCurrent(midlet.formServices);
                }
            }
        }
    }
    
    public void run() {
        while(mTrucking){
            if(mUpdate) {
                // change the alpha value each loop. 255=opaque, 0=transparent.
                mAlpha+= mValue;
                
                if(mAlpha>=255) {
                    //mValue = mValue *-1;
                } else {
                    if(mAlpha<=0) {
                        mValue = mValue *-1;
                    }
                }
                
                // Use the blend method in ImageEffect the change the Alpha value.
                // create a new Image from the new rawInt array.
                ImageUtil.blend(rawInt, mAlpha);
                Image fadingImage = Image.createRGBImage(rawInt, imgTitle.getWidth(), imgTitle.getHeight(), true);
                
                mGraphics.setColor(0xFEFED8);
                mGraphics.drawImage(fadingImage, 0, 0, 0);
                
                mUpdate = false;
            }
            repaint();
        }
    }
}
